#include "GameOverUI.h"
#include "PokerLogic.h"
#include "PokerPlayer.h"
#include "LoadingScene.h"
#include "CardUI.h"
#include "InfoDetailUI.h"

GameOverUI::GameOverUI() : IDialog()
{
    RegDialogCtrl("close", m_close);
    RegDialogCtrl("btn", m_click);
    RegDialogCtrl("btn_detail", m_detail);
    RegDialogCtrl("btn_replay", m_replay);
    RegDialogCtrl("flag_0", m_spUserFlag[0]);
    RegDialogCtrl("flag_1", m_spUserFlag[1]);
    RegDialogCtrl("flag_2", m_spUserFlag[2]);
    RegDialogCtrl("bg_0", m_spUser[0]);
    RegDialogCtrl("bg_1", m_spUser[1]);
    RegDialogCtrl("bg_2", m_spUser[2]);
    RegDialogCtrl("icon_head_0", m_spUserHead[0]);
    RegDialogCtrl("icon_head_1", m_spUserHead[1]);
    RegDialogCtrl("icon_head_2", m_spUserHead[2]);
}

GameOverUI::~GameOverUI(){
}

void GameOverUI::onUILoaded() {
    m_rootNode->setScale(0.5f);
    m_rootNode->runAction(EaseElasticOut::create(ScaleTo::create(0.5f, 1.0f, 1.0f))); //动画
    SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    Size sizeOverlayer = Director::getInstance()->getWinSize();
    Color4B bgColor(0, 0, 0, 200);
    LayerColor* maskColorLayer = LayerColor::create(bgColor, sizeOverlayer.width * 2, sizeOverlayer.height * 2);
    this->addChild(maskColorLayer, -1);
    this->m_click->addClickEventListener([this](Ref* sender) {
        this->m_ntfMgr->postNotification("OnceMore", this);
        DialogManager::shared()->closeAllDialog();
        SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    });
    this->m_close->addClickEventListener([this](Ref* sender) {
        DialogManager::shared()->closeAllDialog();
        PokerLogic::shard()->init();
        Director::getInstance()->replaceScene(LoadingScene::create());
        SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    });
    this->m_detail->addClickEventListener([this](Ref* sender) {
        DialogManager::shared()->showDialog(InfoDetailUI::create());
    });
    this->m_replay->addClickEventListener([this](Ref* sender) {
    });
    this->m_click->setTitleText("");
    this->m_close->setTitleText("");
    this->m_detail->setTitleText("");
    this->m_replay->setTitleText("");
//    this->m_detail->setVisible(false);
    this->m_replay->setVisible(false);
    uint8_t master = PokerLogic::shard()->getMasterSeat();
    uint8_t userCount = PokerLogic::shard()->getMaxPlayerCount();
    std::vector< int32_t > theCards;
    int index = 0;
    uint8_t winUser = 0;
    utility::setSpriteFrame(m_spUserHead[0], "head/vtouxiang_10.png");
    utility::setSpriteFrame(m_spUserHead[1], "head/vtouxiang_07.png");
    utility::setSpriteFrame(m_spUserHead[2], "head/vtouxiang_06.png");
    for (uint8_t i = 0; i < userCount; ++i) {
        std::shared_ptr<PokerPlayer> player = PokerLogic::shard()->getPlayer(i);
        if (player != nullptr) {
            index = 0;
            if(theCards.empty()) {
                winUser = player->getSeat();
                assert(winUser == i);
            }
            m_spUserFlag[i]->setScale(1.2f, 1.2f);
            m_spUserFlag[i]->setVisible(master == i);
            player->getHandCards(theCards);
            for (auto&& card : theCards) {
                CardUI* testUI = CardUI::create();
                testUI->setData(card);
                testUI->setPosition((index++) * 40.0f - 60.0f, -280.0f);
                testUI->setAnchorPoint(Vec2(0.5f, 1.0f));
                testUI->setScale(0.5f);
                m_spUser[i]->addChild(testUI);
            }
        }
    }
//    for (uint8_t i = 0; i < userCount; ++i) {
//        auto win = ((i == winUser) || (winUser != master && i != master));
//        Sprite* sp = Sprite::createWithSpriteFrameName(win ? "gameover/win.png" : "gameover/lose.png");
//        if(sp) {
//            sp->setPosition(160.0f, 30.0f);
//            sp->setScale(0.25f);
//            m_spUser[i]->addChild(sp, 10000);
//        }
//    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    m_rootNode->setPosition(visibleSize.width * 0.5f, visibleSize.height * 0.5f);
}

